package com.china08.yxyapi.api.manage.stu.myclass.classnotice;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.office.YxyNoticeClass;
import com.china08.yxyapi.entity.mg.office.YxyNoticeClass.YxyNotifictionUser;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.YxyNoticeClassRepository;

/**
 * 学生班级通知 修改为 已读状态修改
 * 
 */
@CrossOrigin
@RestController("StuClassnoticeUpdIsReadController")
public class StuClassnoticeUpdIsReadController extends YxyApiBaseController {

    @Autowired
    private YxyNoticeClassRepository yxyNoticeClassRepository;

    @RequestMapping(value = "/api_yw/manage/stu/myclass/classnotice/receive/updRead", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object precess(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "id", required = false) String id) {
        String schoolId = getProfile(request, response).getSchoolId();
        String userId = getProfile(request, response).getId();
        int isRead = 0;
        List<YxyNoticeClass> yxyNoticeClassLst = yxyNoticeClassRepository.findByIdAndSchoolIdAndReceiverUserIdAndReceiverIsRead(id, schoolId, userId, isRead);
        for (YxyNoticeClass yxyNoticeClass : yxyNoticeClassLst) {
            List<YxyNotifictionUser> yxyNotifictionUserList = yxyNoticeClass.getReceiver();
            for (YxyNotifictionUser yxyNotifictionUser : yxyNotifictionUserList) {
                if (yxyNotifictionUser.getIsRead() == 0 && userId.equals(yxyNotifictionUser.getUserId())) {
                    yxyNotifictionUser.setIsRead(1);
                }
            }
        }

        // 計算接收率
        for (YxyNoticeClass yxyNoticeClass : yxyNoticeClassLst) {
            List<YxyNotifictionUser> receiveUsers = yxyNoticeClass.getReceiver();
            if (receiveUsers.size() == 0) {
                yxyNoticeClass.setReceiveRate(0.0);
                continue;
            }
            Integer allReaded = 0;
            for (YxyNotifictionUser notifictionUser : receiveUsers) {
                Integer readed = notifictionUser.isRead;
                if (readed == 1) {
                    allReaded = allReaded + 1;
                }
            }
            double i1 = allReaded;
            double i2 = receiveUsers.size();
            double receiveRate = i1 / i2;
            yxyNoticeClass.setReceiveRate(receiveRate);
        }
        yxyNoticeClassRepository.save(yxyNoticeClassLst);
        RespModel respModel = new RespModel();
        respModel.setMessage("已读");
        return respModel;
    }

    protected static class RespModel extends YxyApiRespModel {

    }
}